var Event = require('../event');
describe('webim 组件测试', function () {
  
  describe('event test', function () {
    var event = new Event();
    it('触发触发事件', function (done) {
      event.addEventListener('test', function (data) {
        expect(data).toBe('test');
        event.removeEventListener('test');
        done();
      });
      event.fire('test', 'test');
    });
    it('removeEventListener 删除指定类型', function (done) {
      event.addEventListener('test1', function (data) {
        console.log(data);
        done(data);
      });
      event.removeEventListener('test1');
      event.fire('test1', 'test');
      expect(event._events['test1'].length).toBe(0);
      done();
    });
    it('removeEventListener 删除指定函数', function (done) {
      function action1(data) {
        done(data);
      }

      function action2(data) {
        console.log('action2');
        expect(data).toBe('test');
        done();
      }

      event.addEventListener('test2', action1);
      event.addEventListener('test2', action2);
      event.removeEventListener('test2', action1);
      event.fire('test2', 'test');
      expect(event._events['test2'].length).toBe(1);
      console.log(event._events);
    });
  });

  describe('dispatcher test', function () {
    var dispatcher = require('../webim/dispatcher');
    it('findOrRegister 测试', function (done) {
      dispatcher.findOrRegister(function (action) {
        switch (action.type) {
        case 'test':
          expect(action.key).toBe(true);
          done();
          break;
        default:
          done('fail');
        }
      });
      dispatcher.dispatch({
        type: 'test',
        key: true
      });
    });
  });
});
